package com.offcn.project.controller;

import com.offcn.dycommon.response.AppResponse;
import com.offcn.project.enums.ProjectImageTypeEnume;
import com.offcn.project.pojo.*;
import com.offcn.project.service.ProjectInfoServcie;
import com.offcn.project.vo.resp.ProjectDetailVo;
import com.offcn.project.vo.resp.ProjectVo;
import com.offcn.utils.OssTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: wzy
 * @Date: 2021/01/25/20:21
 * @Description:
 */
@Api(tags = "项目基本功能模块（文件上传、项目信息获取等）")
@Slf4j
@RequestMapping("/project")
@RestController
public class ProjectInfoController {
    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private ProjectInfoServcie projectInfoServcie;

    @ApiOperation("文件上传功能")
    @PostMapping("/upload")
    public AppResponse<Map<String, Object>> upload(@RequestParam("file") MultipartFile[] files) {
        Map<String, Object> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        try {
            if (files != null && files.length > 0) {
                for (MultipartFile file : files) {
                    if (!file.isEmpty()) {
                        String upload = ossTemplate.upload(file.getInputStream(), file.getOriginalFilename());
                        list.add(upload);
                    }
                }
            }
        } catch (IOException e) {
            AppResponse response = AppResponse.fail(null);
            response.setMsg("上传失败");
            return response;
        }
        map.put("urls", list);
        log.debug("ossTemplate信息：{},文件上传成功访问路径{}", ossTemplate, list);
        System.out.println("上传成功！！");
        return AppResponse.ok(map);

    }

    @ApiOperation(value = "获取项目回报列表")
    @GetMapping("/details/returns/{projectId}")
    public AppResponse<List<TReturn>> getReturnList(@PathVariable("projectId") Integer projectId) {
        List<TReturn> returnList = projectInfoServcie.getReturnList(projectId);
        return AppResponse.ok(returnList);
    }

    @ApiOperation("获取系统所有的项目")
    @GetMapping("/all")
    public AppResponse<List<ProjectVo>> findAllProject() {
        //1.创建集合存储全部项目的vo
        List<ProjectVo> projectVos = new ArrayList<>();
        //2.查询全部项目
        List<TProject> projectList = projectInfoServcie.findAllProject();
        //3.遍历项目编号
        for (TProject project : projectList) {
            //获取项目编号
            Integer id = project.getId();
            //根据项目编号获取项目配图
            List<TProjectImages> images = projectInfoServcie.getProjectImages(id);
            ProjectVo projectVo = new ProjectVo();
            BeanUtils.copyProperties(project, projectVo);
            //遍历项目配图集合
            for (TProjectImages image : images) {
                if (image.getImgtype() == 0) {
                    projectVo.setHeaderImage(image.getImgurl());
                }
            }
            projectVos.add(projectVo);
        }
        return AppResponse.ok(projectVos);
    }

    @ApiOperation(value = "获取项目信息详情")
    @GetMapping("/findProjectInfo/{projectId}")
    @ApiImplicitParam(name = "projectId", value = "项目id", required = true)
    public AppResponse<ProjectDetailVo> findProjectInfo(@PathVariable("projectId") Integer projectId) {
        TProject projectInfo = projectInfoServcie.findProjectInfo(projectId);
        //2.复制属性 Tproject-->ProjectDatailVo
        ProjectDetailVo projectDetailVo = new ProjectDetailVo();
        BeanUtils.copyProperties(projectInfo, projectDetailVo);
        //  //3.设置图片
        List<TProjectImages> imagesList = projectInfoServcie.getProjectImages(projectId);
        if (!CollectionUtils.isEmpty(imagesList)) {
            List<String> detailImageUrl = new ArrayList<>();
            for (TProjectImages projectImages : imagesList) {
                if (projectImages.getImgtype() == ProjectImageTypeEnume.HEADER.getCode().byteValue()) {
                    projectDetailVo.setHeaderImage(projectImages.getImgurl());
                } else {
                    detailImageUrl.add(projectImages.getImgurl());//详细图片
                }
            }
            projectDetailVo.setDetailsImage(detailImageUrl);
        }
        //4.设置回报信息
        List<TReturn> returnList = projectInfoServcie.getReturnList(projectId);
        projectDetailVo.setProjectReturns(returnList);
        return AppResponse.ok(projectDetailVo);
    }

    @ApiOperation(value = "获取所有的项目标签")
    @GetMapping("/findAllTag")
    public AppResponse<List<TTag>> findAllTag() {
        List<TTag> tags = projectInfoServcie.findAllTag();
        return AppResponse.ok(tags);
    }

    @ApiOperation(value = "获取所有的项目分类")
    @GetMapping("/findAllType")
    public AppResponse<List<TType>> findAllType() {
        List<TType> tTypes = projectInfoServcie.findAllType();
        return AppResponse.ok(tTypes);
    }

    @ApiOperation(value = "项目回报信息")
    @GetMapping("/returns/info/{returnId}")
    public AppResponse<TReturn> findReturnInfo(@PathVariable("returnId") Integer returnId) {
        TReturn returnInfo = projectInfoServcie.findReturnInfo(returnId);
        return AppResponse.ok(returnInfo);
    }

}
